package bookstudytest.proxy.jdk.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: Lee
 * @Date: 2019/3/8 0008 13:34
 * @Description:
 */
public class HelloProxy implements InvocationHandler {
    private Object target;

    /**
     * 绑定对象
     *
     * @param target
     * @return
     */
    public Object bind(Object target) {
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }


    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("this proxy invoke");
        // 传入代理对象 和方法参数，类似于反射
        Object invoke = method.invoke(this.target, args);
        System.out.println("this is proxy after");
        return invoke;
    }
}
